{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Version Control and Git"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.  Introduction\n",
    "\n",
    "In any software development, one of the most important tools are **version control software** \n",
    "\n",
    "They are used in virtually all software development and in all environments, by everyone and everywhere.\n",
    "\n",
    "Version control an used on **almost any digital content**, so it is not only restricted to software development, and is also very useful for manuscript files, figures, data and notebooks!\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### There are two main purposes of VCS systems:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1. Keep `track of changes` in the source code.\n",
    "   \n",
    "* Allow `reverting `back to an older revision if something goes wrong.\n",
    "\n",
    "* Work on several `\"branches\"` of the software concurrently.\n",
    "\n",
    "* `Tags revisions` to keep track of which version of the software that was used for what (for example, \"release-1.0\", \"paper-A-final\", ...)\n",
    "\n",
    "#### 2. Make it possible for serveral people to `collaboratively work` on the same code base simultaneously.\n",
    "\n",
    "* Allow many authors to make changes to the code.\n",
    "\n",
    "* Clearly communicating and visualizing changes in the code base to everyone involved."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Basic principles and terminology for VCS systems"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In an VCS, the source code or digital content is stored in a **repository**. \n",
    "\n",
    "* The repository does not only contain the **latest version of all files**, but **the complete history of all changes** to the files since they were added to the repository. \n",
    "\n",
    "\n",
    "* A user can **checkout** the repository, and obtain **a local working copy of the files**. All changes are made to the files in the local working directory, where files can be added, removed and updated. \n",
    "\n",
    "\n",
    "* When a task has been completed, the changes to the local files are **commited** (saved to the repository).\n",
    "\n",
    "\n",
    "* If someone else has been making changes to the same files, a **conflict** can occur. In many cases conflicts can be **resolved** automatically by the system, but in some cases we might manually have to **merge** different changes together.\n",
    "\n",
    "\n",
    "* It is often useful to create a new **branch** in a repository, or a **fork** or **clone** of an entire repository, when we doing larger experimental development. The main branch in a repository is called often **master** or **trunk**. When work on a branch or fork is completed, it can be merged in to the master branch/repository.\n",
    "\n",
    "\n",
    "* With distributed VCSs such as **GIT** or **Mercurial**, we can **pull** and **push** changesets between different repositories. For example, between a local copy of there repository to a central online reposistory,for example on a community repository host site like [github.com](github.com)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2 GIT\n",
    "\n",
    "* Created by ·Linus Torvalds` 2005 ,https://github.com/torvalds\n",
    "\n",
    "### Why git?\n",
    "\n",
    " * Popular (~50% of open source projects)\n",
    " * Truly distributed\n",
    " * Very fast\n",
    " * Everything is local\n",
    " * Free\n",
    " * Safe against corruptions\n",
    " * **GitHub!**\n",
    " \n",
    "### GitHub\n",
    "\n",
    "GitHub is a code hosting platform for version control and collaboration. It lets you and others work together on projects from anywhere.\n",
    "\n",
    "This tutorial teaches you GitHub essentials like repositories, branches, commits, and Pull Requests. You’ll create your own Hello World repository and learn GitHub’s Pull Request workflow, a popular way to create and review code.\n",
    "\n",
    "https://guides.github.com/activities/hello-world/\n",
    "\n",
    "**In the rest of this lecture we will look at `git`.**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.1 Setting Up Git\n",
    "### 2.1.1 Installing git"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### On Windows\n",
    " \n",
    "Download git https://github.com/git-for-windows/git/releases and run the downloaded installer.\n",
    "        \n",
    "#### On Debian/Ubuntu:\n",
    "    \n",
    "```bash\n",
    "$sudo apt install git\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.1.2  Configure the author information:\n",
    "\n",
    "The first time you start to use git, you'll need to configure your author information:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git config --global user.name   yourname\n",
    "!git config --global user.email youemail"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Note:**  if you have the account of Github,Please use the same: user.name and user.email\n",
    "\n",
    "The settings are kept in **\"<GIT_HOME>/etc/gitconfig\"** (of the GIT installed directory) and **\"<USER_HOME>/.gitconfig\"** (of the user's home directory.\n",
    "You can issue\n",
    "```bash\n",
    ">git config --list\n",
    "```\n",
    "to list the settings: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "core.symlinks=false\n",
      "core.autocrlf=true\n",
      "core.fscache=true\n",
      "color.diff=auto\n",
      "color.status=auto\n",
      "color.branch=auto\n",
      "color.interactive=true\n",
      "help.format=html\n",
      "rebase.autosquash=true\n",
      "http.sslbackend=schannel\n",
      "diff.astextplain.textconv=astextplain\n",
      "filter.lfs.clean=git-lfs clean -- %f\n",
      "filter.lfs.smudge=git-lfs smudge -- %f\n",
      "filter.lfs.process=git-lfs filter-process\n",
      "filter.lfs.required=true\n",
      "credential.helper=manager\n",
      "user.name=thermalogic\n",
      "user.email=cmh@seu.edu.cn\n",
      "credential.helper=wincred\n",
      "filter.lfs.smudge=git-lfs smudge -- %f\n",
      "filter.lfs.process=git-lfs filter-process\n",
      "filter.lfs.required=true\n",
      "filter.lfs.clean=git-lfs clean -- %f\n",
      "core.autocrlf=false\n",
      "core.repositoryformatversion=0\n",
      "core.filemode=false\n",
      "core.bare=false\n",
      "core.logallrefupdates=true\n"
     ]
    }
   ],
   "source": [
    "!git config --list"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3 Getting Started with Local Repo\n",
    "\n",
    "There are 2 ways to start a Git-managed project:\n",
    "\n",
    "* **Starting your own project**;\n",
    "\n",
    "* Cloning an existing project from a GIT host.\n",
    "\n",
    "Here, We shall begin with **Starting your own project**,then manage the project under Git, add  files, and commit it to the repo.\n",
    "\n",
    "### 3.1 Setup the Working Directory for a New Project\n",
    "\n",
    "Let's start a programming project under the working directory called **gitdemo** with one source file **Hello.c** under **D:/PySEE/**\n",
    "\n",
    "The working directory: D:/PySEE/gitdemo\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%cd D:/PySEE/\n",
    "%mkdir gitdemo\n",
    "%cd gitdemo\n",
    "%pwd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The source file **Hello.c** in **gitdemo** "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%file ./hello.c\n",
    "\n",
    "#include <stdio.h>  \n",
    " \n",
    "int main() {                   \n",
    "    printf(\"C Hello, world!\\n\");  \n",
    "    return 0;                   \n",
    "}                             "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  3.2 Manage the project under Git - Initialize a new local Git Repo "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To manage a project under Git, run \"git init\" at the project root directory \n",
    "```bash\n",
    ">git init \n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%cd D:/PySEE/gitdemo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# manage the project under Git\n",
    "!git init "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A **hidden** sub-directory called **.git** will be created under your project root directory (as shown in the above **\"ls\"** listing), which contains ALL Git related data.\n",
    "![git-repo](./img/git/git-repo.jpg)\n",
    "Take note that **EACH Git repo** is associated with **a project directory** (and its sub-directories). The Git repo is completely contain within the project directory. Hence, it is safe to copy, move or rename the project directory. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.3 Cloning a Project from a Remote Repo\n",
    "\n",
    "You may **\"git clone <remote-url>\"** to copy from an existing remote project"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we want to fork or clone an existing repository, we can use the command \n",
    "```bash\n",
    ">git clone repository\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git clone https://github.com/PySEE/home"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Git clone can take a URL to a public repository, like above, or a path to a local directory:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git clone gitdemo gitdemo2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4  Adding files,Status and committing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.1 Add a new file\n",
    "\n",
    "To add a new file to the repository, we first create the file and then use the command:\n",
    "```bash\n",
    ">git add filename\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git add hello.c"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.2 Status"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using the command \n",
    "```base\n",
    ">git status\n",
    "```\n",
    "we get a summary of the current status of the **working directory**. It shows if we have modified, added or removed files."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git status"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this case, after having added the file `hello.c`, the command `git status` list it as an *untracked* file and has not yet been **commited** to the **repository**.\n",
    "\n",
    "It is therefore not in the repository."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.3 Commit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git commit -m \"added hello.c file\"   hello.c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git status"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.3 Add a python file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%file ./hello.py\n",
    "\n",
    "print('Hello,World,Python!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git add hello.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git commit -m \"added python file\" hello.py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "After *committing* the change to the **repository** from **the local working directory**, `git status` again reports that working directory is clean."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git status "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.5 Commiting changes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When files that is tracked by GIT are changed, they are listed as *modified* by `git status`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%file ./hello.c\n",
    "\n",
    "#include <stdio.h>  \n",
    " \n",
    "int main() {                   \n",
    "    printf(\"C Hello, world!\\n\"); \n",
    "    // new line\n",
    "    printf(\"Test Commiting changes!\\n\"); \n",
    "    return 0;                   \n",
    "}       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git status"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Again, we can commit such changes to the repository using the `git commit -m \"message\"` command."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git commit -m \"added one more line in hello.c\"  hello.c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git status"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5 File Status\n",
    "\n",
    "### 5.1 Git Storage Model\n",
    "\n",
    "A file could be `untracked` or `tracked`.\n",
    "\n",
    "As mentioned, Git tracks file changes at commits.\n",
    "\n",
    "In Git, changes for a `tracked` file could be:\n",
    "\n",
    "* 1 `unstaged` (in **Working Tree**)` - called unstaged changes,\n",
    "\n",
    "* 2 `staged` (in **Staging Area** or Index or **Cache**)` - called staged changes, or\n",
    "\n",
    "* 3 `committed` (in **local repo** object database)`.\n",
    "\n",
    "**Git Storage Model**\n",
    "\n",
    "![GitStorageModel](./img/git/Git_StorageModel.png)\n",
    "\n",
    "\n",
    "The files in **\"working tree\"** or **\"staging area\"** could have status of `unmodified, added, modified, deleted, renamed, copied`, as reported by `\"git status\"`.\n",
    "\n",
    "The `\"git status\"` output is divided into 3 sections:\n",
    "\n",
    "* `\"Changes not staged for commit\"` for the unstaged changes in \"working tree\", \n",
    "\n",
    "* `\"Changes to be committed\"` for the staged changes in the \"staging area\", \n",
    "\n",
    "* and `\"Untracked files\".` \n",
    "\n",
    "In each section, It lists all the files that have been changed, i,e., files having status other than unmodified.\n",
    "\n",
    "### 5.2 When a `new` file is created in the working tree, \n",
    "\n",
    "* it is marked as **new** in working tree and shown as an **untracked** file. \n",
    "\n",
    "* When the file change is **staged**, it is marked as `new (added)` in the staging area, and `unmodified` in working tree. \n",
    "\n",
    "* When the file change is **committed**, it is marked as `unmodified` in both the working tree and staging area.\n",
    "\n",
    "![Git_FileNew](./img/git/Git_FileNew.png)\n",
    "\n",
    "### 5.3 When a `committed` file is modified,\n",
    "\n",
    "* it is marked as `modified` in the working tree and `unmodified` in the staging area.\n",
    "\n",
    "* When the file change is `staged`, it is marked as `modified` in the staging area and `unmodified` in the working tree. \n",
    "\n",
    "* When the file change is `committed`, it is marked as `unmodified` in both the working tree and staging area.\n",
    "\n",
    "![GitFileModified](./img/git/Git_FileModified.png)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6 Removing files"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To remove file that has been added to the repository, use \n",
    "```bash\n",
    "git rm filename, \n",
    "```\n",
    "which works similar to ```git add filename```:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%file ./tmpfile\n",
    "\n",
    "A short-lived file."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Add it:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git add tmpfile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git commit -m \"adding file tmpfile\" tmpfile "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%ls"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Remove it again:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git rm tmpfile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git commit -m \"remove file tmpfile\" tmpfile "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%ls"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7 Commit logs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The messages that are added to the commit command are supposed to give a short (often one-line) description of the changes/additions/deletions in the commit. If the `-m \"message\"` is omitted when invoking the `git commit` message an editor will be opened for you to type a commit message (for example useful when a longer commit message is requried). \n",
    "\n",
    "We can look at **the revision log** by using the command `git log`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git log"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the commit log, each version is shown with a `timestampe`, a `unique` has tag that, and `author` information and the commit message.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8 Diffs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "All commits results in a changeset, which has a **diff** describing the changes to the file associated with it. We can use `git diff` so see what has changed in a file:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%file ./hello.c\n",
    "\n",
    "#include <stdio.h>  \n",
    " \n",
    "int main() {                   \n",
    "    printf(\"Hello, world!\\n\"); \n",
    "    // new line\n",
    "    printf(\"Test Commiting change!\\n\");\n",
    "    // new line\n",
    "    printf(\"Test diff!\\n\");\n",
    "    return 0;                   \n",
    "}       \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git diff hello.c"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That looks quite cryptic but is a standard form for describing changes in files. We can use other tools, like graphical user interfaces or web based systems to get a more easily understandable diff.\n",
    "\n",
    "In **VS code**, it can look like this:\n",
    "\n",
    "![git-code-diff](./img/git/git-code-diff.jpg)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9 Discard changes in the working directory"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To discard a change (revert to the latest version of the file in the repository) we can use the `checkout` command like this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git checkout -- hello.c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git status"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 10 Branching"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With branches we can create diverging code bases in the same repository. They are for example useful for experimental development that requires a lot of code changes that could break the functionality in the master branch. Once the development of a branch has reached a stable state it can always be merged back into the trunk. Branching-development-merging is a good development strategy when serveral people are involved in working on the same code base. But even in single author repositories it can often be useful to always keep the master branch in a working state, and always branch/fork before implementing a new feature, and later merge it back into the main trunk.\n",
    "\n",
    "### 10.1 Create a new branch\n",
    "\n",
    "In GIT, we can create a new branch:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git branch test1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can list the existing branches like this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git branch"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And we can **switch between branches** using `checkout`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git checkout test1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Make a change in the new branch."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%file ./hello.c\n",
    "\n",
    "#include <stdio.h>  \n",
    " \n",
    "int main() {                   \n",
    "    printf(\"Hello, world!\\n\"); \n",
    "    // new line\n",
    "    printf(\"Test !\\n\");\n",
    "    // new line\n",
    "    printf(\"Test Commiting changes!\\n\");\n",
    "     // new line\n",
    "    printf(\"Test test1 branch!\\n\");\n",
    "    return 0;                   \n",
    "}   \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git commit -m \"added a line in expr1 branch\" hello.c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git branch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git checkout master"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git branch"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Your current working version hello.c in master branch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# %load ./hello.c\n",
    "\n",
    "#include <stdio.h>  \n",
    " \n",
    "int main() {                   \n",
    "    printf(\"C Hello, world!\\n\"); \n",
    "    // new line\n",
    "    printf(\"Test Commiting changes!\\n\"); \n",
    "    return 0;                   \n",
    "}       "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Return to test1 branch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git checkout test1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git branch"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Your current working version hello.c in test1 branch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# %load ./hello.c\n",
    "\n",
    "#include <stdio.h>  \n",
    " \n",
    "int main() {                   \n",
    "    printf(\"Hello, world!\\n\"); \n",
    "    // new line\n",
    "    printf(\"Test !\\n\");\n",
    "    // new line\n",
    "    printf(\"Test Commiting changes!\\n\");\n",
    "     // new line\n",
    "    printf(\"Test test1 branch!\\n\");\n",
    "    return 0;                   \n",
    "}   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 10.2  Merge the existing branch \n",
    "\n",
    "We can merge an existing branch and all its changesets into another branch (for example the master branch) like this:\n",
    "\n",
    "**1** First change to the target branch:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git checkout master"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**2** then, merge to the target branch:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git merge test1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git branch "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Your current working version `hello.c` in `master` branch merged with `test1` branch "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# %load ./hello.c\n",
    "\n",
    "#include <stdio.h>  \n",
    " \n",
    "int main() {                   \n",
    "    printf(\"Hello, world!\\n\"); \n",
    "    // new line\n",
    "    printf(\"Test !\\n\");\n",
    "    // new line\n",
    "    printf(\"Test Commiting changes!\\n\");\n",
    "     // new line\n",
    "    printf(\"Test test1 branch!\\n\");\n",
    "    return 0;                   \n",
    "}   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "you can delete the branch `test1` now that it has been merged into the master:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git branch -d test1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git branch"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 11 Pulling and pushing change sets between repositories"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 11.1 Hosted repositories"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[Github.com](Github.com) is a git repository hosting site that is very popular with both open source projects (for which it is free) and private repositories (for which a subscription might be needed).\n",
    "\n",
    "With a hosted repository it easy to collaborate with colleagues on the same code base, and you get a graphical user interface where you can browse the code and look at commit logs, track issues etc. \n",
    "\n",
    "Creat the repo in your github within the name,which is the same as your local repo\n",
    " \n",
    "**Note** the repo must is empty,without README.md\n",
    "\n",
    "#### 1 Add the repo at github as the remote origin of the locale git repo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git remote add origin https://github.com/your-username/gitdemo.git "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git remote"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2 push"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "After making changes to our local repository, we can push changes to a remote repository using `git push`. Again, the default target repository is `origin`, so we can do:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git status"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%file ./hello.py\n",
    "\n",
    "print('Hello,World,Python!')\n",
    "print('Test Push!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git add hello.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git commit -m \"added python file\" hello.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git push -u origin master"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3 Pull"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can retrieve updates from the origin repository by \"pulling\" changesets from \"origin\" to our repository:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git pull origin"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "you may shallowly clone the branch of repository for saving bandwidth"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git clone --depth 1 -b master https://github.com/your-username/gitdemo.git "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 12 Graphical user interfaces"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are also a number of graphical users interfaces for GIT. \n",
    "\n",
    "* **GitHub Desktop** \n",
    "  [Download here](https://desktop.github.com/)\n",
    " \n",
    "  \n",
    "* **Git in Visual Studio Code** \n",
    "  If you have installed Git and Visual Studio Code [Download here](https://code.visualstudio.com/)   \n",
    "\n",
    "We strongly recommend that you use version control for your projects."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Further reading"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* http://git-scm.com/book\n",
    "\n",
    "* How to get started with GIT and work with GIT Remote Repo  http://www3.ntu.edu.sg/home/ehchua/programming/howto/Git_HowTo.html\n",
    "\n",
    "* 知乎：怎样使用GitHub. http://www.zhihu.com/question/20070065"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.7"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "307.2px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  },
  "widgets": {
   "state": {},
   "version": "1.1.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
